package com.powernode.controller;

import com.powernode.constant.CommonConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@Api(tags = "登出管理")
@RequestMapping("sys/logout")
public class LogoutController {

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 核心目的 将redis的token删除
     *
     * @return
     */
    @PostMapping
    @ApiOperation("登出操作")
    public ResponseEntity<Void> doLogout(HttpServletRequest request) {
        String header = request.getHeader(CommonConstant.AUTHORIZATION);
        String jwt = header.replaceFirst(CommonConstant.BEARER, "");
        // 删除
        redisTemplate.delete(CommonConstant.AUTH_TOKEN_PREFIX + jwt);
        return ResponseEntity.ok().build();
    }


}
